package com.qzl.common.netty.handler.strategy;

import com.qzl.common.common.RespConstant;
import com.qzl.common.exception.ChatException;
import com.qzl.common.netty.annotation.MessageType;
import com.qzl.common.netty.config.ChatConfig;
import com.qzl.common.netty.config.UserConfig;
import com.qzl.common.netty.handler.MessageHandler;
import com.qzl.common.netty.message.Message;
import com.qzl.common.netty.model.UserModel;
import com.qzl.common.netty.utis.ResponseUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author: qzl
 * @date : 2025/6/23 17:32
 * @description:
 */
@MessageType(messageType = ChatConfig.LOGIN_MESSAGE)
@Slf4j
public class LoginHandler implements MessageHandler {
    @Override
    public void handle(ChannelHandlerContext ctx, Message message) {
        log.info("开始处理登录消息:{}", message);
        UserModel userModel = message.getUserModel();
        String token = UserConfig.login(userModel, ctx);
        if (Objects.equals(token, RespConstant.USERNAME_PASSWORD_ERROR.getMessage())){
            ctx.fireExceptionCaught(new ChatException(ChatConfig.LOGIN_MESSAGE, RespConstant.USERNAME_PASSWORD_ERROR.getMessage()));
            return;
        }
        message.setToken(token);
        message.setContent("登录成功");
        message.setMessageType((int) ChatConfig.LOGIN_MESSAGE);
        ResponseUtils.sendChannelResponse(ctx, message);
    }
}
